package com.qj.yantaishan.controller;

import java.util.List;
import com.baomidou.mybatisplus.extension.service.IService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qj.yantaishan.common.annotation.Log;
import com.qj.yantaishan.common.enums.BusinessType;
import com.qj.yantaishan.domain.ProductToGroup;
import com.qj.yantaishan.service.IProductToGroupService;
import com.qj.yantaishan.common.core.controller.BaseController;
import com.qj.yantaishan.common.core.domain.AjaxResult;
import com.qj.yantaishan.common.utils.poi.ExcelUtil;
import com.qj.yantaishan.common.core.page.TableDataInfo;

/**
 * 商品分组关联Controller
 * 
 * @author qihe
 * @date 2022-07-22
 */
@RestController
@RequestMapping("/yantaishan/ProductToGroup")
public class ProductToGroupController extends BaseController
{
    private String prefix = "yantaishan/ProductToGroup";

    @Autowired
    private IProductToGroupService productToGroupService;

    @RequiresPermissions("yantaishan:group:view")
    @GetMapping()
    public String group()
    {
        return prefix + "/group";
    }

    /**
     * 查询商品分组关联列表
     */
    @RequiresPermissions("yantaishan:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProductToGroup productToGroup)
    {
        startPage();
        List<ProductToGroup> list = productToGroupService.selectProductToGroupList(productToGroup);
        return getDataTable(list);
    }

    /**
     * 导出商品分组关联列表
     */
    @RequiresPermissions("yantaishan:group:export")
    @Log(title = "商品分组关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProductToGroup productToGroup)
    {
        List<ProductToGroup> list = productToGroupService.selectProductToGroupList(productToGroup);
        ExcelUtil<ProductToGroup> util = new ExcelUtil<ProductToGroup>(ProductToGroup.class);
        return util.exportExcel(list, "商品分组关联数据");
    }

    /**
     * 新增商品分组关联
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存商品分组关联
     */
    @RequiresPermissions("yantaishan:group:add")
    @Log(title = "商品分组关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProductToGroup productToGroup)
    {
        return toAjax(productToGroupService.insertProductToGroup(productToGroup));
    }

    /**
     * 修改商品分组关联
     */
    @RequiresPermissions("yantaishan:group:edit")
    @GetMapping("/edit/{productId}")
    public String edit(@PathVariable("productId") Long productId, ModelMap mmap)
    {
        ProductToGroup productToGroup = productToGroupService.selectProductToGroupByProductId(productId);
        mmap.put("productToGroup", productToGroup);
        return prefix + "/edit";
    }

    /**
     * 修改保存商品分组关联
     */
    @RequiresPermissions("yantaishan:group:edit")
    @Log(title = "商品分组关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProductToGroup productToGroup)
    {
        return toAjax(productToGroupService.updateProductToGroup(productToGroup));
    }

    /**
     * 删除商品分组关联
     */
    @RequiresPermissions("yantaishan:group:remove")
    @Log(title = "商品分组关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(productToGroupService.deleteProductToGroupByProductIds(ids));
    }
}
